﻿using System;
using System.Windows;
using AppfabricAdministrator.Commands;

namespace AdminMainUI
{
    /// <summary>
    /// Interaction logic for ClusterAccess.xaml
    /// </summary>
    public partial class ClusterAccess : Window
    {
        private PowershellCommands command = null;
        public ClusterAccess()
        {
            InitializeComponent();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
        }

        public ClusterAccess(PowershellCommands commands)
        {
            InitializeComponent();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
            command = commands;
            BindAccounts();
        }

        public void ShowAccessWindow()
        {
            this.ShowDialog();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            String user = txtUser.Text.Trim();
            if (String.IsNullOrEmpty(user))
            {
                MessageBox.Show("Enter an account name!");
            }
            else
            {
                command.GrantAccess(user);
                BindAccounts();
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (lstAccounts.SelectedItem != null)
            {
                command.RevokeAccess(lstAccounts.SelectedItem as String);
                BindAccounts();
            }
        }

        private void BindAccounts()
        {
            lstAccounts.ItemsSource = command.GetAllowedAccounts();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
